package com.hs.jhzd.controller;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hs.jhzd.entity.Configs;
import com.hs.jhzd.repository.ConfigsRepository;

import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Controller
@RequestMapping("/cir")
@Slf4j
public class CriminalImageRecognitionController {

  @Autowired
  private ConfigsRepository configsRepository;

  @RequestMapping("/rebackInfo")
  @ResponseBody
  public boolean rebackInfo(String rybh, Long contentId) {
    log.debug("【线索举报】获取全局配置");
    List<Configs> configses = configsRepository.findAll();
    Configs configs = CollectionUtils.isEmpty(configses) ? null : configses.get(0);
    if (configs == null) {
      log.debug("【线索举报】获取到的全局配置为空，无法给实战发送请求");
      return false;
    }
    String hongsenApi = configs.getHongsenApi();
    if (!StringUtils.startsWith(hongsenApi, "http://")) {
      hongsenApi = "http://" + hongsenApi;
    }
    if (!StringUtils.endsWith(hongsenApi, "/")) {
      hongsenApi += "/";
    }
    hongsenApi += "rebackInfo?rybh=" + rybh + "&contentId=" + contentId;
    log.debug("【线索举报】获取到实战的api地址->{}", hongsenApi);
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder().url(hongsenApi).get().build();

    try {
      Response response = client.newCall(request).execute();
      log.debug("【线索举报】实战返回->{},{}", response.code(), response.message());
      boolean success = response.code() == 200;
      if (success) {
        log.debug("【线索举报】发送给实战成功！");
      } else {
        log.debug("【线索举报】发送给实战失败！");
      }
      return success;
    } catch (IOException e) {
      log.error("【线索举报】发送给实战出现异常！", e);
      e.printStackTrace();
    }

    return false;
  }



}
